package com.wish.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wish.dto.RoleDto;
import com.wish.entity.SysDept;
import com.wish.entity.SysRole;
import com.wish.entity.SysRoleDept;
import com.wish.entity.SysRoleMenu;
import com.wish.service.ISysDeptService;
import com.wish.service.ISysRoleDeptService;
import com.wish.service.ISysRoleMenuService;
import com.wish.service.ISysRoleService;
import com.wish.vo.ResultEntity;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author 五组
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/wish/sys-role")
public class SysRoleController {

    //角色
    @Autowired
    private ISysRoleService sysRoleService;
    //部门
    @Autowired
    ISysDeptService sysDeptService;

    @Autowired
    ISysRoleMenuService sysRoleMenuService;

    //角色部门中间
    @Autowired
    ISysRoleDeptService sysRoleDeptService;

    //role and dept 列表展示 使用的是多堆多查询
    @RequestMapping("/rolelist")
    public ResultEntity list(RoleDto roleDto){

        PageInfo<SysRole> list = sysRoleService.getlist(roleDto);
        return ResultEntity.ok("200","成功",list);
    }

    //这个是添加
    @RequestMapping("/roleadd")
    public ResultEntity add(@RequestBody SysRole sysRole){
        try {
            Date date = new Date();
            sysRole.setCreateTime(date);
            sysRoleService.save(sysRole);
            for (Long mid:sysRole.getMids()){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(mid);
                sysRoleMenuService.save(sysRoleMenu);
            }
            return ResultEntity.ok("200","成功");
        }catch (Exception e){
            return ResultEntity.error("500","错误");
        }
    }

    //查询部门
    @RequestMapping("getDept")
    public ResultEntity getDept(){
        List<SysDept> list = sysDeptService.list();
        return ResultEntity.ok("200","部门查询成功",list);
    }
    //这里删除
    @RequestMapping("del")
    public ResultEntity del(Long[] ids){
        try{
            for (Long id:ids
            ) {
                sysRoleService.removeById(id);
                QueryWrapper<SysRoleDept> wrapper = new QueryWrapper<>();
                wrapper.eq("role_id",id);
                sysRoleDeptService.remove(wrapper);
            }
            return ResultEntity.ok("200","成功");
        }catch (Exception e){
            return ResultEntity.error("500","错误");
        }
    }

    //修改
    @RequestMapping("/roleupdate")
    public ResultEntity update(@RequestBody SysRole sysRole){
        try {
            sysRoleService.updateById(sysRole);
            QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",sysRole.getRoleId());
            sysRoleMenuService.remove(wrapper);
            for (Long mid:sysRole.getMids()){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(sysRole.getRoleId());
                sysRoleMenu.setMenuId(mid);
                sysRoleMenuService.save(sysRoleMenu);
            }
            return ResultEntity.ok("200","成功");
        }catch (Exception e){
            return ResultEntity.error("500","错误");
        }
    }
    //回显
    @RequestMapping("getOne")
    public SysRole getOne(Integer id){
        SysRole sysRole = sysRoleService.getById(id);
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",id);
        List<SysRoleMenu> list = sysRoleMenuService.list(wrapper);
        for (SysRoleMenu srm:list) {
            System.out.println(srm+"-=-=-=-=-=-==-=-=--=-");
        }
        sysRole.setMLists(list);
        return sysRole;
    }

    @RequestMapping("getrlist")
    public List<SysRole> getrlist(Integer did){
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_id",did);
        List<SysRole> list = sysRoleService.list(wrapper);
        return list;
    }

}
